#include "stdafx.h"
#include "ckCheckModelManagerAccess.h"

namespace ck
{

	CheckModelManagerAccess::CheckModelManagerAccess()
	{
	}


	CheckModelManagerAccess::~CheckModelManagerAccess()
	{
	}

	void CheckModelManagerAccess::BuildGetModelsSQL(char* szSQL, size_t nSize)
	{
		const char* format = "SELECT ID, CK_CODE, CK_NAME, CK_PRODUCT_CODE, CK_PRODUCT, CK_DESCRIPTION FROM %s";
		snprintf(szSQL, nSize, format, CK_MODEL_TABLE_MODEL);
	}

	void CheckModelManagerAccess::BuildGetModelSQL(char* szSQL, size_t nSize, const char* szName)
	{
		const char* format = "SELECT ID, CK_CODE, CK_NAME, CK_PRODUCT_CODE, CK_PRODUCT, CK_DESCRIPTION FROM %s WHERE CK_NAME='%s'";
		snprintf(szSQL, nSize, format, CK_MODEL_TABLE_MODEL, szName);
	}

	void CheckModelManagerAccess::BuildGetModelSQL(char* szSQL, size_t nSize, z_uint32 nCode)
	{
		//const char* format = "SELECT ID, CK_CODE, CK_NAME, CK_PRODUCT_CODE, CK_PRODUCT, CK_DESCRIPTION FROM %s WHERE CK_CODE=%d";
		const char* format = "SELECT ID, CK_CODE, CK_NAME, CK_PRODUCT_CODE, CK_PRODUCT, CK_DESCRIPTION FROM %s WHERE ID=%d";
		snprintf(szSQL, nSize, format, CK_MODEL_TABLE_MODEL, nCode);
	}

	void CheckModelManagerAccess::BuildInsertModelSQL(char* szSQL, size_t nSize, z_uint32 code, const char* szCode, const char* szName, z_uint32	productCode, const char* szProduct, const char* szDscription)
	{
		const char* format = "INSERT INTO %s (CK_CODE,CK_NAME,CK_PRODUCT_CODE,CK_PRODUCT,CK_DESCRIPTION) VALUES(%d,'%s',%d,'%s','%s')";

		snprintf(szSQL, nSize, format, CK_MODEL_TABLE_MODEL, code, szName, productCode, szProduct, szDscription);
	}

	void CheckModelManagerAccess::BuildModelAddItemSQL(char* szSQL, size_t nSize, z_uint32 nModelID, z_uint32 nCheckItemCode)
	{
		const char* format = "INSERT INTO %s (CK_MODEL_ID,CK_CK_ITEM_CODE) VALUES(%d,%d)";

		snprintf(szSQL, nSize, format, CK_MODEL_TABLE_ITEM, nModelID, nCheckItemCode);
	}

	void CheckModelManagerAccess::BuildModelRemoveItemSQL(char* szSQL, size_t nSize, z_uint32 nModelID, z_uint32 nCheckItemCode)
	{
		const char* format = "DELETE FROM %s WHERE CK_MODEL_ID=%d AND CK_CK_ITEM_CODE=%d";

		snprintf(szSQL, nSize, format, CK_MODEL_TABLE_ITEM, nModelID, nCheckItemCode);
	}

	void CheckModelManagerAccess::BuildModelHasItemSQL(char* szSQL, size_t nSize, z_uint32 nModelID, z_uint32 nCheckItemCode)
	{		
		const char* format = "SELECT COUNT(*) AS COUNTS FROM %s WHERE CK_MODEL_ID=%d AND CK_CK_ITEM_CODE=%d";		

		snprintf(szSQL, nSize, format, CK_MODEL_TABLE_ITEM, nModelID, nCheckItemCode);
	}

	void CheckModelManagerAccess::BuildGetOperatorsSQL(char* szSQL, size_t nSize, z_uint32 nModelCode, z_uint32 nCheckItemCode)
	{
		const char* format = "select DISTINCT(a.CK_OPER_CODE) as CK_OPER_CODE, a.CK_MOD_CODE as CK_MOD_CODE from CK_ELEMENT_OPERATOR a, CK_MODEL_ITEM b where a.CK_ITEM_CODE=b.CK_CK_ITEM_CODE and b.CK_MODEL_CODE=%d and b.CK_CK_ITEM_CODE=%d";

		snprintf(szSQL, nSize, format, nModelCode, nCheckItemCode);
	}

	void CheckModelManagerAccess::BuildGetCheckItemCodeSQL(char* szSQL, size_t nSize, z_uint32 nModelCode)
	{
		const char* format = "select CK_CK_ITEM_CODE FROM %s WHERE CK_MODEL_ID=%d";

		snprintf(szSQL, nSize, format, CK_MODEL_TABLE_ITEM, nModelCode);
	}
}


